package com.example.fruit.ui.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.fruit.bean.Fruit;
import com.example.fruit.bean.User;
import com.example.fruit.ui.activity.BrowseActivity;
import com.example.fruit.ui.activity.CollectActivity;
import com.example.fruit.ui.activity.FruitUnpaidActivity;
import com.example.fruit.ui.activity.FruitPaidActivity;
import com.example.fruit.ui.activity.FruitRefundActivity;
import com.example.fruit.bean.Orders;
import com.example.fruit.ui.activity.MainActivity;
import com.example.fruit.ui.activity.ManageActivity;
import com.example.fruit.MyApplication;
import com.example.fruit.R;
import com.example.fruit.ui.activity.LoginActivity;
import com.example.fruit.ui.activity.OpeningActivity;
import com.example.fruit.ui.activity.PasswordActivity;
import com.example.fruit.ui.activity.PersonActivity;
import com.example.fruit.util.SPUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 个人中心Fragment
 */
public class UserFragment extends Fragment {
    private Activity mActivity;
    private List<Orders> mOrder = new ArrayList<>();
    private LinearLayout llPerson;
    private LinearLayout llSecurity;
    private LinearLayout llFavorite;
    private LinearLayout update;
    private LinearLayout manage;
    private Button btnLogout;
    private ImageView bt_daishouhuo;
    private ImageView bt_daifukuan;
    private ImageView bt_daifahuo;
    private ImageView bt_pingjia;
    private ImageView bt_tuikuan;
    private TextView Browse;
    private TextView name;
    private TableLayout taborder;
    private RelativeLayout taborder2;
    private String account;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user,container,false);
        llPerson = view.findViewById(R.id.person);
        llSecurity = view.findViewById(R.id.security);
        update = view.findViewById(R.id.update);
        manage = view.findViewById(R.id.manage);
        btnLogout = view.findViewById(R.id.logout);
        llFavorite = view.findViewById(R.id.favorite);
        bt_daifukuan =view.findViewById(R.id.daifukuan);
        bt_daifahuo =view.findViewById(R.id.daifahuo);
        bt_daishouhuo =view.findViewById(R.id.daishouhuo);
        bt_pingjia =view.findViewById(R.id.pingjia);
        bt_tuikuan =view.findViewById(R.id.tuikuan);
        taborder =view.findViewById(R.id.taborder);
        taborder2 =view.findViewById(R.id.taborder2);
        Browse =view.findViewById(R.id.Browse);
        name =view.findViewById(R.id.name);
        initView();
        return view;
    }

    private void initView() {
        Boolean isAdmin = (Boolean) SPUtils.get(mActivity,SPUtils.IS_ADMIN,false);
        account = (String) SPUtils.get(mActivity, SPUtils.ACCOUNT, "");
        taborder.setVisibility(isAdmin?View.GONE:View.VISIBLE);
        taborder2.setVisibility(isAdmin?View.GONE:View.VISIBLE);
        llFavorite.setVisibility(isAdmin?View.GONE:View.VISIBLE);//联系客服
        manage.setVisibility(isAdmin?View.VISIBLE:View.GONE);

        //浏览记录
        Browse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转页面
                 Intent intent = new Intent(mActivity, BrowseActivity.class);
                startActivity(intent);
            }
        });
       //待付款
        bt_daifukuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转页面
                Intent intent = new Intent(mActivity, FruitUnpaidActivity.class);
                startActivity(intent);
            }
        });
        //待发货
        bt_daifahuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转页面
                Intent intent = new Intent(mActivity, FruitPaidActivity.class);
                startActivity(intent);
            }
        });
        //待收货
        bt_daishouhuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转页面
                Intent intent = new Intent(mActivity, CollectActivity.class);
                startActivity(intent);
            }
        });
        //评价
        bt_pingjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转页面
              Intent intent = new Intent(mActivity, FruitRefundActivity.class);
                startActivity(intent);
            }
        });
        //售后
        bt_tuikuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到客服
                mOrder = DataSupport.where("account = ? " ,account).find(Orders.class);//查询全部
                if (mOrder !=null && mOrder.size()>0 ){
                    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                    builder.setIcon(R.drawable.a1);//设置图标
                    builder.setTitle("水果商城");//设置对话框的标题
                    builder.setMessage("即将跳转到在线客服，是否确认？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ProgressDialog dialog3 = new ProgressDialog(mActivity);
                                    dialog3.setMessage("正在连接客服···");
                                    dialog3.show();
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                Toast.makeText(mActivity,"如无法联系，请添加好友后再试!", Toast.LENGTH_LONG).show();
                                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqapi://card/show_pslcard?src_type=internal&source=sharecard&version=1&uin=1632957243")));//跳转到QQ资料
                                            }catch (Exception e){
                                                Toast.makeText(mActivity,"请安装最新版QQ后使用此功能!", Toast.LENGTH_LONG).show();
                                            }
                                            dialog3.dismiss();
                                        }
                                    }, 1500);

                                }
                            }
                    );
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                }
                            }
                    );
                    AlertDialog a = builder.create();
                    a.show();

            }else {
                    Toast.makeText(mActivity,"您还没有订单哦，请您下单后再联系官方在线客服，如要咨询商品详情，请点击商品后在详情界面点击左下角客服图标咨询!", Toast.LENGTH_LONG).show();
                }

            }
        });
        //个人信息
        llPerson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转页面
                Intent intent = new Intent(mActivity, PersonActivity.class);
                startActivity(intent);
            }
        });
        //用户管理
        manage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转页面
                Intent intent = new Intent(mActivity, ManageActivity.class);
                startActivity(intent);
            }
        });
        //账号安全
        llSecurity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转页面
                Intent intent = new Intent(mActivity, PasswordActivity.class);
                startActivity(intent);
            }
        });
        //版本更新
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProgressDialog dialog = new ProgressDialog(mActivity);
                dialog.setMessage("正在查找最新版本···");
                dialog.show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Uri uri = Uri.parse("https://pan.baidu.com/s/1Be7qNuB6O5LQKgrE7szmog");
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);
                        dialog.dismiss();
                        Toast.makeText(mActivity,"发现新版本", Toast.LENGTH_LONG).show();
                    }
                }, 2000);
            }
        });
        //联系电话
        llFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //呼叫
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                builder.setIcon(R.drawable.a1);//设置图标
                builder.setTitle("水果商城");//设置对话框的标题
                builder.setMessage("  确认呼叫24小时服务热线？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                    try {
                                        //呼叫
                                        callPhone("4007777777");
                                        Toast.makeText(mActivity,"点击拨号键呼叫24小时免费服务热线", Toast.LENGTH_LONG).show();
                                    } catch (Exception e) {
                                        //异常处理
                                    }
                                }
                        }
                );
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        }
                );
                AlertDialog a = builder.create();
                a.show();
        }});
        //退出登录
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                builder.setIcon(R.drawable.a1);//设置图标
                builder.setTitle("水果商城");//设置对话框的标题
                builder.setMessage("提示：退出后将清空您的账号信息");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                MyApplication.Instance.getMainActivity().finish();
                                SPUtils.remove(mActivity,SPUtils.IS_ADMIN);
                                SPUtils.remove(mActivity,SPUtils.ACCOUNT);
                                startActivity(new Intent(mActivity, LoginActivity.class));
                            }});
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }});
                AlertDialog a = builder.create();
                a.show();
            }});
    }
    public void callPhone(String str){
        Intent intent=new Intent(Intent.ACTION_DIAL);
        Uri data =Uri.parse("tel:"+str);
        intent.setData(data);
        startActivity(intent);
    }
}
